<?php

namespace App\Http\Controllers\TK_Api;

use App\Http\Requests\TK\UseCdkeyRequest;
use App\Models\Course;
use App\Models\Knowledge;
use App\Models\KnowledgePost;
use App\Models\Questions;
use App\Service\TK\Course\CourseService;
use DB;
use Exception;
use Illuminate\Http\JsonResponse;

class CourseController extends ApiBaseController
{

    /**
     * 获取学科列表
     * @return JsonResponse|object
     */
    public function list() {
        $uid = isset($this->user) ? $this->user->id : 0;

        $list[] = [
            'title' => '初中',
            'children' => Course::where('tiku_course.studyType', '=', 1)
                ->orderBy('tiku_course.sortNo')
                ->leftJoin('tiku_user_has_course', function ($join) use($uid) {
                    $join->on('tiku_course.id', '=', 'tiku_user_has_course.courseId')
                        ->where('tiku_user_has_course.userId', '=', $uid);
                })
                ->selectRaw('tiku_course.id, tiku_course.studyName, tiku_course.courseName, tiku_course.fullName, tiku_course.paperPrice, tiku_course.questionPrice, if(tiku_user_has_course.status and tiku_user_has_course.status=1,true,false) as has')
                ->get()
        ];
        $list[] = [
            'title' => '高中',
            'children' => Course::where('tiku_course.studyType', '=', 2)
                ->orderBy('tiku_course.sortNo')
                ->leftJoin('tiku_user_has_course', function ($join) use($uid) {
                    $join->on('tiku_course.id', '=', 'tiku_user_has_course.courseId')
                        ->where('tiku_user_has_course.userId', '=', $uid);
                })
                ->selectRaw('tiku_course.id, tiku_course.studyName, tiku_course.courseName, tiku_course.fullName, tiku_course.paperPrice, tiku_course.questionPrice, if(tiku_user_has_course.status and tiku_user_has_course.status=1,true,false) as has')
                ->get()
        ];
        return $this->restful($list);
    }


    /**
     * 切换学科后的数据
     * @return JsonResponse|object
     */
    public function switch() {
        $course = Course::find(request()->input('courseId'));
        if (!$course) return $this->restful(null, '没有找到学科', false, 404);

        $data = [
            'id' => $course->id,
            'studyName' => $course->studyName,
            'courseName' => $course->courseName,
            'fullName' => $course->fullName,
            'paperPrice' => $course->paperPrice,
            'questionPrice' => $course->questionPrice,
            'has' => isset($this->user) && DB::table('tiku_user_has_course')->where('userId', $this->user->id)->where('courseId', $course->id)->first(),
            'knowledge' => Knowledge::where('level', 1)
                ->where('courseId', $course->id)
                ->where('status', 0)
                ->select(['id', 'parentId', 'courseId', 'level', 'knowledgeName as name', 'qCount as zsdCount'])
                ->get()
                ->toArray(),
        ];

        return $this->restful($data);
    }

    /**
     * 使用激活码
     * @param UseCdkeyRequest $request
     * @param CourseService $courseService
     * @return JsonResponse|object
     * @throws Exception
     */
    public function useCdkey(UseCdkeyRequest $request, CourseService $courseService) {
        $params = $request->validated();

        $result = $courseService->buyCourse($this->user, 2, [
            'cdkey' => $params['code'],
            'courseId' => $params['courseId'],
        ]);

        if (!$result) return $this->restful(null, '激活失败', false);

        return $this->restful();
    }

    /**
     * 获取学科下所有题目ids
     * @return JsonResponse|object
     */
    public function getQuestionIds() {
        $courseId = request('id');
        if (!$courseId) return $this->restful(null, '无效学科', false);
        $list = Questions::where('courseId', $courseId)->pluck('id');

        return $this->restful($list);
    }
}
